import javax.swing.*;
import java.awt.*;

// 成绩对话窗
public class ScoreDialog extends JDialog {
    public ScoreDialog(MainWindow frame) {
        super(frame, true); // 调用父类构造方法,阻塞父窗体
        int[] scores = ScoreRecorder.getScores(); // 获取当前前三名成绩
        JPanel scoreP = new JPanel(new GridLayout(4, 1));
        scoreP.setBackground(Color.WHITE);
        JLabel title = new JLabel("得分排行榜", JLabel.CENTER); // 标题标签，居中
        title.setFont(new Font("黑体", Font.BOLD, 20));
        title.setForeground(Color.RED);
        JLabel first = new JLabel("第一名：" + scores[2], JLabel.CENTER);
        JLabel second = new JLabel("第二名：" + scores[1], JLabel.CENTER);
        JLabel third = new JLabel("第三名：" + scores[0], JLabel.CENTER);

        JPanel tool = getjPanel(frame);

        scoreP.add(title);
        scoreP.add(first);
        scoreP.add(second);
        scoreP.add(third);

        Container c = getContentPane(); // 获取主容器
        c.setLayout(new BorderLayout()); // 使用边界布局
        c.add(scoreP, BorderLayout.CENTER); // 成绩面板放中间
        add(tool, BorderLayout.SOUTH); // 将按钮添加到面板的顶部

        setTitle("游戏结束"); // 对话框标题

        int width, height; // 对话框宽高
        width = height = 200; // 对话框宽高均为200
        // 获得主窗体中居中位置的横坐标
        int x = frame.getX() + (frame.getWidth() - width) / 2;
        // 获得主窗体中居中位置的纵坐标
        int y = frame.getY() + (frame.getHeight() - height) / 2;
        setBounds(x, y, width, height); // 设置坐标和宽高
        setVisible(true); // 显示对话框
    }

    private JPanel getjPanel(MainWindow frame) {
        JButton restart = new JButton("重新开始"); // 重新开始按钮
        // 按钮添加事件监听
        restart.addActionListener(e -> { // 当点击时
            FreshThread.fresh = FreshThread.FRESH;
            dispose(); // 销毁对话框
        });

        JButton returnButton = new JButton("返回");
        returnButton.addActionListener(e -> {
            frame.initFirst();
            dispose();
        });

        JPanel tool = new JPanel();  //面板对象
        tool.setLayout(new FlowLayout(FlowLayout.CENTER ));//放按钮的JPanel设置为流式布局,居中对齐

        //将三个按钮添加到面板对象
        tool.add(restart);
        tool.add(returnButton);
        return tool;
    }
}